/**
* file name  : it/ciano/jenoware/ID.java
* authors    : Luciano Xumerle
* created    : dom 05 set 2004 10:00:14 CEST
*
* Copyright (c) 2004-2005 Luciano Xumerle. All rights reserved.
*
* This file is part of gephinfo.
*
* gephinfo is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
*/

package it.ciano.jenoware;

import java.lang.Float;

/**
 * With class ID is possible to sort the subject's ids.
 *
 * @author Luciano Xumerle
 * @version 0.0.1
 */
public class ID implements Comparable
{
    // GLOBAL PRIVATE VARS
    private String _id = null; // identifier
    private String _w1 = null;
    private Float _n;
    private String _w2 = null;

    /**
     * Class Constructor.
     *
     * @param id a valid subject id ([string]integer[.integer][string])
     */
    public ID ( String id )
    {
        char[] ch = id.toCharArray ();
        boolean isOK = true;
        boolean dotadd = true;
        String w1 = "";
        String n = "0";
        String w2 = "";
        for ( int i = 0; i < ch.length; ++i )
        {
            if ( isOK )
            {
                if ( Character.isLetter ( ch[ i ] ) )
                {
                    if ( n.equals ( "0" ) )
                        w1 += ch[ i ];
                    else
                        w2 += ch[ i ];
                }
                else if ( Character.isDigit ( ch[ i ] ) )
                {
                    if ( w2.equals ( "" ) )
                        n += ch[ i ];
                    else
                        w2 += ch[ i ];
                }
                else if ( ch[ i ] == '.' || ch[ i ] == ',' )
                {
                    if ( Character.isDigit ( ch[ i - 1 ] ) && dotadd )
                    {
                        n += ".";
                        dotadd = false;
                    }
                    else if ( w2.equals ( "" ) && n.equals ( "0" ) )
                        w1 += ch[ i ];
                    else
                        w2 += ch[ i ];
                }
                else
                {
                    isOK = false;
                    w1 = id;
                    n = "0";
                    w2 = "";
                }
            }
        }
        // INITIALIZING PRIVATE VARS
        _id = id;
        _w1 = w1;
        _n = new Float ( n );
        _w2 = w2;
    }


    /**
     * Class Constructor.
     * Attention: the method has no checks on data.
     *
     * @param id a valid subject id ([string]integer[.integer][string])
     * @param a the number prefix string (may be empty).
     * @param b the number.
     * @param c the number suffix string (may be empty).
     */
    public ID ( String id, String a, Float b, String c )
    {
        _id = id;
        _w1 = a;
        _n = b;
        _w2 = c;
    }


    /**
     * Returns the stored ID.
     *
     * @return the ID.
     */
    final public String getID()
    {
        return _id;
    }

    /**
     * Overriding toString method
     *
     * @return the stored id
     */
    public String toString()
    {
        return getID();
    }

    /**
     * Overriding equals method
     *
     * @param o the object to compare
     * @return true or false
     */
    public boolean equals ( Object o )
    {
        ID oth = ( ID ) o;
        return ( _id.equals ( oth._id ) );
    }

    /**
     * Overriding clone method
     *
     * @return a new cloned object ID
     */
    public Object clone()
    {
        ID cloned = new ID ( _id );
        return ( Object ) cloned;
    }

    /**
     * Overriding compareTo method
     *
     * @param o the object to compare
     * @return an integer less or equal or major of 0
     */
    public int compareTo ( Object o )
    {
        ID oth = ( ID ) o;
        if ( this._w1.equals ( oth._w1 ) )
        {
            if ( this._n.equals ( oth._n ) )
                return ( this._w2.compareTo ( oth._w2 ) );
            else
                return ( this._n.compareTo ( oth._n ) );
        }
        else
            return ( this._w1.compareTo ( oth._w1 ) );
    }

} // end class

